<?php
/**
 * Groups module for XOOPS
 *
 * You may not change or alter any portion of this comment or credits
 * of supporting developers from this source code or any supporting source code 
 * which is considered copyrighted (c) material of the original comment or credit authors.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright       The Groups Project http://code.google.com/p/groupsforxoops/
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
 * @package         groups
 * @since           2.3.0
 * @author          Hui Xiao < xh.8326@gmail.com >
 * @version         $Id: group.php Sat Jul 18 12:39:49 CST 2009
 */

include "conf.inc";

$group_id = isset($_GET["id"]) ? intval($_GET["id"]) : 0 ;

$op = isset($_GET["o"]) ? trim($_GET["o"]) : "" ;
$start = isset($_GET["start"]) ? intval($_GET["start"]) : 0 ;

/** 加载相关的类文件 **/
$group_handler = xoops_getmodulehandler("group");
$memb_handler = xoops_getmodulehandler("memb");
$footprint_handler = xoops_getmodulehandler("footprint");
$event_handler = xoops_getmodulehandler("event");
$threads_handler = xoops_getmodulehandler("threads");
$column_handler = xoops_getmodulehandler("column");


/** 检查来访者权限 **/
if ( empty($group_id) ) header("Location: index.php");
$group_obj = $group_handler->getGroup($group_id);
if ( !is_object($group_obj) || $group_obj->isNew() )	redirect_header("index.php",5,_GP_GROUP_NOEXIST);

if ( $xoopsUser ) {
    $params = array(
        'group_id'=>(int)$group_id,
        'uid'=>(int)$xoopsUser->uid()
    );
    if ( !in_array($xoopsUser->uid(),$group_obj->getVar('group_footprint_uids')) ) {
        $group_handler->updateFootprintUids($group_id,$xoopsUser->uid());
    }
    $footprint_handler->setFootprint($params,true);
    unset($params);
}

/** 加载模板文件 **/
$xoopsOption["xoops_pagetitle"] = $group_obj->getVar("group_name","n") ;
$xoopsOption["template_main"] = "groups_group.html";
include "group_header.php";

$subTemplate = "";
switch ($op) {
    case "p":
    case "post":
        $ac = isset($_GET["a"]) ? trim($_GET["a"]) : "";
        $tid = isset($_GET["tid"]) ? intval($_GET["tid"]) : 0;
        $posts_handler = xoops_getmodulehandler("posts");
        switch ($ac) {
            case "r":
            case "reply":
                
                break;
                
            case "q":
            case "quote":
                
                break;
                
            case "e":
            case "edit":
                break;
                
            case "c":
            case "create":
            default:
                $post_obj = $posts_handler->create();
                $mode = "post";
                $params = $post_obj->getValues();
        }
        include_once(dirname(__FILE__)."/include/form.post.topic.php");
        $subTemplate = "posting";
        break;
        
    case "e":
    case "event":
        $limit = 30;
        $extra = "id={$group_id}&o={$op}";
        $params = array(
            "criteria" => array(
                "group_id"=>$group_id,
            ),
            "start" => $start,
            "limit" => $limit,
            "sort"  => "event_create_time",
            "order" => "DESC",
            "extra" => $extra
        );
        
        $objs = $event_handler->getEvents($params);
        $pagenav = $event_handler->getPageNav($params);
        $events = array();
        if ( !empty($objs) ) {
            foreach ( $objs as $k=>$obj ) {
                $events[$k] = $obj->getValues();
            }
        }
        $xoopsTpl->assign(array("events"=>$events,"pagenav"=>$pagenav));
        $subTemplate = "event";
        break;
    
    case "f":
    case "forum":
        $limit = 30;
        $column_id = isset($_GET["cid"]) ? intval($_GET["cid"]) : 0 ;
        
        $extra = "id={$group_id}&o={$op}";
        $params = array(
            "criteria" => array(
                "group_id" => $group_id,
                "threads_status" => 0,
            ),
            "start" => $start,
            "limit" => $limit,
            "sort"  => array("threads_displaytop"=>"DESC","threads_update_time"=>"DESC"),
            "order" => "DESC",
            "extra" => $extra
        );
        
        if ( !empty($column_id) ) {
            $params["criteria"]["column_id"] = $column_id;
            $params["extra"] .= "&cid={$column_id}";
        }
        
        $objs = $threads_handler->getThreads($params); 
        $pagenav = $threads_handler->getPageNav($params);
        
        $user_ids = $column_ids = $columns = array();
        if ( !empty($objs) ) {
            foreach ( $objs as $k=>$obj ) {
                $user_ids[] = $obj->getVar("uid");
                if ( $obj->getVar("threads_last_uid") ) {
                    $user_ids[] = $obj->getVar("threads_last_uid");
                }
                if ( $obj->getVar("column_id") ) {
                    $column_ids[] = $obj->getVar("column_id");
                }
            }
            $user_ids = array_unique($user_ids);
            $column_ids = array_unique($column_ids);
        }
        unset($params);
        if ( $column_ids ) {
            $params = array(
                "criteria" => array(
                    "group_id" => $group_id,
                    "column_id" => $column_ids,
                )
            );
            $columns = $column_handler->getColumns($params,false);
        }
        
        if ( $user_ids ) {
            $users = xoGroups::getUsers($user_ids);
        }

        $threads = $_list = array();
        if ( !empty($objs) ) {
            $threads["tops"] = $threads["list"] = array();
            foreach ( $objs as $k=>$obj ) {
                $_list[$k] = $obj->getValues();
                $_list[$k]["column_name"] = $obj->getVar("column_id") ? $columns[$obj->getVar("column_id")] : "";
                $_list[$k]["author_nickname"] = $obj->getVar("uid") ? $users[$obj->getVar("uid")]["name"] : "";
                $_list[$k]["poster_nickname"] = $obj->getVar("threads_last_uid") ? $users[$obj->getVar("threads_last_uid")]["name"] : "";
                $_list[$k]["create_time"] = $obj->createTime();
                $_list[$k]["update_time"] = $obj->updateTime("c");
                $_list[$k]["threads_icon"] = $obj->icon();
                $_list[$k]["author_avatar"] = $obj->getVar("uid") ? $users[$obj->getVar("uid")]["avatar"] : "";
                $_list[$k]["poster_avatar"] = $obj->getVar("threads_last_uid") ? $users[$obj->getVar("threads_last_uid")]["avatar"] : "";
                $_list[$k]["author_online"] = $obj->getVar("uid") ? $users[$obj->getVar("uid")]["online"] : "";
                $_list[$k]["poster_online"] = $obj->getVar("threads_last_uid") ? $users[$obj->getVar("threads_last_uid")]["online"] : "";
                if ( $_list[$k]["threads_displaytop"] ) {
                    $threads["tops"][$k] = $_list[$k];
                } else {
                    $threads["list"][$k] = $_list[$k];
                }
            }
        }
        
        $xoopsTpl->assign( array("threads"=>$threads,"pagenav"=>$pagenav) );
        $subTemplate = "forum";
        break;
    
    case "t":
    case "footprint":
        $limit = 30 ;
        $extra = "id={$group_id}&o={$op}";
        $params = array(
            "criteria" => array(
                "group_id"=>$group_id,
                "footprint_display"=>1,
            ),
            "fields"=> array("uid"),
            "start" => $start,
            "limit" => $limit,
            "sort"  => "footprint_update_time",
            "order" => "DESC",
            "extra" => $extra
        );
        
        if ( $xoopsUser && $group_obj->uid() == $xoopsUser->uid() ) {
            unset($params["criteria"]["footprint_display"]);
        }
        
        $objs = $footprint_handler->getFootprints($params);
        $pagenav = $footprint_handler->getPageNav($params);
        $user_ids = array();
        if ( !empty($objs) ) {
            foreach ( $objs as $k=>$obj ) {
                $user_ids[] = $obj->getVar("uid");
            }
        }
        
        $members = $user_ids ? xoGroups::getUsers($user_ids) : array();
        $xoopsTpl->assign(array("members"=>$members,"pagenav"=>$pagenav));
        $subTemplate = "footprint";
        break;
    
    case "m":
    case "member":
        $limit = 30 ;
        $extra = "id={$group_id}&o={$op}";
        $params = array(
            "criteria" => array(
                "group_id"=>$group_id,
                "member_status" => 1,
            ),
            "fields"=> array("uid"),
            "start" => $start,
            "limit" => $limit,
            "sort"  => "member_create_time",
            "order" => "DESC",
            "extra" => $extra
        );
        
        if ( $xoopsUser && $group_obj->uid() == $xoopsUser->uid() ) {
            unset($params["criteria"]["member_status"]);
        }
        
        $objs = $memb_handler->getMembers($params);
        $pagenav = $memb_handler->getPageNav($params);
        
        $user_ids = array();
        if ( !empty($objs) ) {
            foreach ( $objs as $k=>$obj ) {
                $user_ids[] = $obj->getVar("uid");
            }
        }
        $members = $user_ids ? xoGroups::getUsers($user_ids) : array();
        $xoopsTpl->assign(array("members"=>$members,"pagenav"=>$pagenav));
        $subTemplate = "member";
        break;
    
    default:
        // get group members
        $memb_uids = $group_obj->getVar('group_member_uids');
        $memb_uids = _subarglast($memb_uids,10);
        // get group footprint
        $tog_uids = $foot_uids = $group_obj->getVar('group_footprint_uids');
        $foot_uids = _subarglast($foot_uids,10);
        
        $_uids = $memb_uids + $foot_uids ;
        $_uids[] = $group_obj->uid();
        $_uids = array_values(array_unique($_uids));
        $_users = $foot_uids ? xoGroups::getUsers($_uids) : array();
        $memb_users = $foot_users = array();
        if ( $_users ) {
            foreach ( $memb_uids as $v ) {
                if ( isset($_users[$v] ) ) {
                    $memb_users[$v] = $_users[$v];
                }
            }
            foreach ( $foot_uids as $v ) {
                if ( isset($_users[$v] ) ) {
                    $foot_users[$v] = $_users[$v];
                }
            }
        }
        
        // get group to groups ids
        $tog_uids = _randarg($tog_uids,10);
        $group_ids = $footprint_handler->getGroupIds($tog_uids);
        if ( $group_ids ) {
            foreach ( $group_ids as $k=>$v ) {
                if ( $v == $group_id ) unset($group_ids[$k]);
            }
        }
        $group_ids = _randarg($group_ids,10);
        $params = array(
            'criteria' => array(
                'group_id' => $group_ids,
//                'group_status' => 1
            ),
            'fields' => array('group_name','group_logo'),
            "sort"  => "group_foot_total",
            "order" => "DESC",
        );
        $togroups = $group_handler->getGroups($params,false);
        $xoopsTpl->assign(array(
            "memb_users"=>$memb_users,
            "foot_users"=>$foot_users,
            "togroups"=>$togroups,
            "creator"=> $_users[$group_obj->uid()]['name']
        ));
        $subTemplate = "index";
        
}

// get this group info
$group = $group_obj->info();
$xoopsTpl->assign(array("group"=>$group,"subTemplate"=>$subTemplate));

include "footer.php";

function _randarg(array $arg, $num=10) {
    if ( count($arg) <= $num ) return $arg;
    $key_ids = array_rand($arg,$num);
    $_ids = array();
    foreach ( $key_ids as $k=>$v ) {
        $_ids[] = $arg[$v];
    }
    unset($arg);
    return $_ids;
}

function _subarglast(array $arg, $num=10) {
    if ( count($arg) <= $num ) return $arg;
    $ret = array_splice($arg,count($arg)-$num,count($arg));
    return $ret;
}
?>
